package com.example.mardan.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.mardan.entity.Couplet;
import com.example.mardan.retrofit.RetrofitClient;
import com.example.mardan.service.CoupletApiService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CoupletViewModel extends ViewModel {
    private MutableLiveData<Couplet> coupletLiveData = new MutableLiveData<>();

    public LiveData<Couplet> getCouplet(String apiKey) {
        CoupletApiService service = RetrofitClient.getClient().create(CoupletApiService.class);
        Call<Couplet> call = service.getCouplet(apiKey);
        call.enqueue(new Callback<Couplet>() {
            @Override
            public void onResponse(Call<Couplet> call, Response<Couplet> response) {
                if (response.isSuccessful() && response.body() != null) {
                    coupletLiveData.setValue(response.body());
                } else {
                    coupletLiveData.setValue(null); // or handle error
                }
            }

            @Override
            public void onFailure(Call<Couplet> call, Throwable t) {
                coupletLiveData.setValue(null); // or handle error
            }
        });
        return coupletLiveData;
    }
}